package controllers

import (
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"hr/logic"
	"hr/models"
	"net/http"
)

// SignUpHandler 注册
func SignUpHandler(c *gin.Context) {
	var p models.ParamSignUp
	if err := c.ShouldBind(&p); err != nil {
		zap.L().Error("登录参数异常", zap.Error(err))
		error, ok := err.(validator.ValidationErrors)
		if !ok {
			c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
			return
		}
		c.JSON(http.StatusBadRequest, gin.H{"error": removeTopStruct(error.Translate(trans))})
		return
	}
	err := logic.SingUp(&p)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "注册失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"msg": "注册成功"})
}

// LoginHandler 登录
func LoginHandler(c *gin.Context) {
	p := new(models.ParamLogin)
	if err := c.ShouldBindJSON(p); err != nil {
		zap.L().Error("参数错误", zap.Error(err))
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			c.JSON(http.StatusOK, gin.H{
				"msg": err.Error(),
			})
		}
		c.JSON(http.StatusOK, gin.H{
			"msg": removeTopStruct(errs.Translate(trans)),
		})
		return
	}
}
